<script setup lang="ts">
import { ref, onMounted } from "vue";
import vue3data from "./datas/vue3.data";
import ItemView from "./components/ItemView.vue";
import Fuse from "fuse.js";
type ItemObj = { itemname: string; itemvalue: string };
const data = ref<ItemObj[]>();
const fuse = new Fuse(vue3data, {
  keys: ["itemname"],
  threshold: 0.0,
});
onMounted(() => {
  data.value = vue3data;
});
const getValByWord = (event: any) => {
  let results = fuse.search(`${event}`);
  console.log(results);
  let arr: any[] = [];
  results.forEach((result) => {
    arr.push(result.item);
  });
  data.value = arr;
  if (!event) {
    data.value = vue3data;
  }
};
</script>

<template>
  <div class="box">
    <lay-input
      prefix-icon="layui-icon-search"
      @input="getValByWord"
      allow-clear
    ></lay-input>
    <div class="item-container">
      <lay-empty v-if="data?.length!<=0" description="努力开发中..."></lay-empty>
      <item-view
        v-for="(item, index) in data"
        :itemname="item.itemname"
        :itemvalue="item.itemvalue"
        :key="index"
      ></item-view>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.box {
  margin: 10px;
  height: calc(100vh - 20px);
  display: flex;
  flex-direction: column;
}
.item-container {
  display: flex;
  flex: 1;
  flex-direction: column;
  overflow-y: auto;
  scroll-behavior: smooth;
  & > * {
    flex-shrink: 0;
  }
}
.item-container::-webkit-scrollbar {
  width: 3px;
  height: 1px;
}
.item-container::-webkit-scrollbar-thumb {
  border-radius: 3px;
  background-color: skyblue;
  background-image: -webkit-linear-gradient(
    45deg,
    rgba(255, 255, 255, 0.2) 25%,
    transparent 25%,
    transparent 50%,
    rgba(255, 255, 255, 0.2) 50%,
    rgba(255, 255, 255, 0.2) 75%,
    transparent 75%,
    transparent
  );
}
.item-container::-webkit-scrollbar-track {
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  background: #ededed;
  border-radius: 3px;
}
</style>
